package com.youlu.campus.admin.ca.controller;

import com.youlu.campus.admin.ca.dto.UserCaExcel;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.ExcelUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.UserCaRecord;
import com.youlu.campus.entity.VO.req.UserCaQueryVO;
import com.youlu.campus.service.ca.impl.UserCaRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class QueryCaController {

    @Autowired
    private UserCaRecordService userCaRecordService;

    /**
     * 证书查询-第三方
     *
     * @param userCaQuery
     * @return
     */
    @PostMapping("/user-ca/query")
    public RestResult userQuery(@RequestBody UserCaQueryVO userCaQuery) {
        String userId = AuthUtils.getUserId();
        userCaQuery.setUserId(userId);
        return RestResultBuilder.builder().success().data(userCaRecordService.userCaQuery(userCaQuery)).build();
    }

    /**
     * 证书查询
     *
     * @param userCaQuery
     * @return
     */
    @PostMapping("/user-ca/list")
    public RestResult list(@RequestBody UserCaQueryVO userCaQuery) {
        return RestResultBuilder.builder().success().data(userCaRecordService.list(userCaQuery)).build();
    }

    /**
     * 接口
     * @param file
     * @return
     */
    @PostMapping("/user-ca/import")
    public RestResult importExcel(@RequestParam("file") MultipartFile file) {
        List<UserCaExcel> excelDataList = ExcelUtils.readExcel(file, UserCaExcel.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        List<UserCaRecord> list = excelDataList.stream().map(ed -> {
            UserCaRecord userCaRecord = new UserCaRecord();
            BeanUtils.copyProperties(ed, userCaRecord);
            String caOrganizationId = ed.getCaOrganizationId();
            userCaRecord.setImportCaOrgId(caOrganizationId);
            userCaRecord.setCreatedTime(new Date());
            return userCaRecord;
        }).collect(Collectors.toList());
        userCaRecordService.saveBatch(list);
        return RestResultBuilder.builder().success(true).build();
    }

    @GetMapping("/user-ca/detail/{id}")
    public RestResult detail(@PathVariable String id){
        return RestResultBuilder.builder().data(userCaRecordService.detail(id)).success(true).build();
    }

    @PostMapping("/user-ca/update")
    public RestResult update(@RequestBody UserCaRecord userCaRecord){
        userCaRecordService.update(userCaRecord);
        return RestResultBuilder.builder().success(true).build();
    }

    @DeleteMapping("/user-ca/delete/{id}")
    public RestResult delete(@PathVariable String id){
        userCaRecordService.remove(id);
        return RestResultBuilder.builder().success(true).build();
    }




}
